/**
 * 
 */
package org.istt.export;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.istt.domain.FileBoSungCCHN;
import org.istt.model.NotificationBean;

/**
 * @author Ndtho
 *
 */
public class BmQT_Notify extends BaseReport{
	
	public void saveToMSWord(InputStream docFile, String outputFile, NotificationBean f) throws IOException {
	    
	    XWPFDocument docx = new XWPFDocument(docFile);
	    Iterator<XWPFTable> range=docx.getTablesIterator();

	    //Header
        XWPFTable tableHeader=range.next();
        
        //Form Detail
        XWPFTable tableContact=range.next();
        
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		Date today = new Date();
		String date =  dateFormat.format(today);
        
        /*==================== HEADER ====================*/
        XWPFTableRow header_row = tableHeader.getRow(1);
        XWPFRun header_column = header_row.getCell(0).getParagraphs().get(0).createRun();
        setTextFormat(header_column, getText("report.BmQT1201.header.date.first")+ " Ng\u00E0y "+date.substring(0, 2)+" th\u00E1ng "+date.substring(3, 5)+" n\u0103m "+date.substring(6, 10), FONT_SIZE_12, false, true, FONT_TIMES_NEW_ROMAN);

        /*==================== FORM DETAIL ====================*/
        XWPFTableRow row_0 = tableContact.getRow(0);
        setTextFormat(row_0.getCell(0).getParagraphs().get(0).createRun(), f.getContent(), FONT_SIZE_12, true, true, FONT_TIMES_NEW_ROMAN);
        
	    //write to word file
	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    docx.write(baos);
	    try {
	    	File file = new File(outputFile);
	    	FileOutputStream foStream = new FileOutputStream(file);
	    	baos.writeTo(foStream);
	    	foStream.flush();
	    	
	    	System.out.println("Create File Successful!");
	    	
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	public InputStream toMSWord(InputStream docFile, NotificationBean bean) throws IOException {
	    
	    XWPFDocument docx = new XWPFDocument(docFile);
	    Iterator<XWPFTable> range=docx.getTablesIterator();

	  //Header
        XWPFTable tableHeader=range.next();
        
        //Form Detail
        XWPFTable tableContact=range.next();
        
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		Date today = new Date();
		String date =  dateFormat.format(today);
        
        /*==================== HEADER ====================*/
        XWPFTableRow header_row = tableHeader.getRow(1);
        XWPFRun header_column = header_row.getCell(0).getParagraphs().get(0).createRun();
        setTextFormat(header_column, getText("report.BmQT1201.header.date.first")+ " Ng\u00E0y "+date.substring(0, 2)+" th\u00E1ng "+date.substring(3, 5)+" n\u0103m "+date.substring(6, 10), FONT_SIZE_12, false, true, FONT_TIMES_NEW_ROMAN);

        /*==================== FORM DETAIL ====================*/
        XWPFTableRow row_0 = tableContact.getRow(0);
        setTextFormat(row_0.getCell(0).getParagraphs().get(0).createRun(), bean.getContent(), FONT_SIZE_12, true, true, FONT_TIMES_NEW_ROMAN);
        
	    //write to word file
	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    docx.write(baos);
	    
	    return new ByteArrayInputStream(baos.toByteArray());
	}
}
